package com.market.plus.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.market.plus.controller.mapper.JsonMapping;
import com.market.plus.core.domain.NewsSecurityType;
import com.market.plus.core.domain.NewsType;
import com.market.plus.core.dto.CommentDto;
import com.market.plus.core.dto.NewsDto;
import com.market.plus.core.dto.ParticipantDto;
import com.market.plus.core.service.NewsService;
import com.market.plus.core.service.ParticipantService;

@Controller
public class NewsController {

   public static final String STATUS_PAGE = "news/status";
   public static final String STATUS_COMMENT_PAGE = "news/comment/status";

   private NewsService newsService;

   private ParticipantService participantService;

   private JsonMapping jsonMapping;

   @Autowired
   public NewsController(@Qualifier("newsService") NewsService newsService,
         @Qualifier("participantService") ParticipantService participantService,
      @Qualifier("jsonMapping") JsonMapping jsonMapping) {
      this.newsService = newsService;
      this.participantService = participantService;
      this.jsonMapping=jsonMapping;
   }

   @RequestMapping(value = "/saveContent/{newsId}/introduction", method = RequestMethod.POST)
   public String saveIntrodution(@PathVariable("newsId")Long newsId,@ModelAttribute("news")NewsDto newsDto, ModelMap model) throws Exception{
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      newsDto.setNewsType(NewsType.PERSONAL.getNewsType());
      newsDto.setTitle("");
      if (newsId > 0)
         newsDto.setId(newsId);
      else 
         newsDto.setId(null);
      this.newsService.saveNews(newsDto, participant.getId(), NewsSecurityType.ALLFRIENDS, null);
      return STATUS_PAGE;
   }

   @RequestMapping(value = "/saveContent/new", method = RequestMethod.POST)
   public @ResponseBody JSONObject saveNews(@ModelAttribute("news")NewsDto newsDto,ModelMap model) throws Exception{
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      newsDto.setNewsType(NewsType.NEWS.getNewsType());
      newsDto.setId(null);
      newsDto.setTitle("");
      String guid = this.newsService.saveNews(newsDto, participant.getId(), NewsSecurityType.ALLFRIENDS, null);
      JSONObject jsonObject = jsonMapping.saveNewsStatus(guid);
      return jsonObject;
   }

   @RequestMapping(value = "/news/common", method = RequestMethod.GET)
   public @ResponseBody JSONArray getNews() throws Exception {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      List<NewsDto> newsDtos = this.newsService.getCommonNews(participant.getId());
      JSONArray jsonArray = this.jsonMapping.convertNews(newsDtos);
      return jsonArray;
   }

   @RequestMapping(value = "/news/private", method = RequestMethod.GET)
   public @ResponseBody JSONArray getPrivate() throws Exception {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      List<NewsDto> newsDtos = this.newsService.getPrivateNews(participant.getId());
      JSONArray jsonArray = this.jsonMapping.convertNews(newsDtos);
      return jsonArray;
   }

   @RequestMapping(value = "/news/{newsGUID}/comments", method = RequestMethod.GET)
   public @ResponseBody JSONArray getComments(@PathVariable("newsGUID")String newsGUID) throws Exception {
      List<CommentDto> commentDtos = this.newsService.getComments(newsGUID);
      JSONArray jsonArray = this.jsonMapping.convertComments(commentDtos);
      return jsonArray;
   }

   @RequestMapping(value = "/news/{newsId}/comment", method = RequestMethod.POST)
   public String addComment(@PathVariable("newsId")Long newsId, @ModelAttribute("comment")CommentDto comment,ModelMap model) throws Exception {
      ParticipantDto participant = this.participantService.findLoginUserName(true);
      this.newsService.addComment(participant.getId(), newsId, comment);
      return STATUS_COMMENT_PAGE;
   }
}
